import UIKit
import Flutter

@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {

    var weixinResult:FlutterResult? = nil
    override func application(
        _ application: UIApplication,
        didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
    ) -> Bool {
        GeneratedPluginRegistrant.register(with: self)

        //------------------------------------
        let flutter:FlutterViewController? = self.window.rootViewController as? FlutterViewController;

        if let binaryMessenger = flutter?.engine?.binaryMessenger {
            // 单向发收通信
            let getChannel = FlutterMethodChannel(name: "FtoO", binaryMessenger: binaryMessenger)
            let sendChannel = FlutterMethodChannel(name: "OtoF", binaryMessenger: binaryMessenger)
            //            sendChannel.invokeMethod("send_to_f", arguments: ["data": "原生发送消息到flutter"])
            getChannel.setMethodCallHandler { call, result in
                if let jsonData = try? JSONSerialization.data(withJSONObject: call.arguments ?? [], options: []) {
                    if let jsonString = String(data: jsonData, encoding: .utf8) {
                        let alert = UIAlertController(title: "原生弹窗", message: "接收的method：\(call.method)，参数\(jsonString)", preferredStyle: UIAlertController.Style.alert)
                        alert.addAction(UIAlertAction(title: "发送消息", style: UIAlertAction.Style.destructive, handler: { _ in
                            sendChannel.invokeMethod("send_to_f", arguments: ["data": "原生发送消息到flutter"])
                            result(FlutterMethodNotImplemented)
                        }))
                        alert.addAction(UIAlertAction(title: "回调消息", style: UIAlertAction.Style.default, handler: { _ in
                            result(["data": "原生回调的消息"])
                        }))
                        alert.addAction(UIAlertAction(title: "不回调消息", style: UIAlertAction.Style.cancel, handler: { _ in
                            result(FlutterMethodNotImplemented)
                        }))
                        flutter?.present(alert, animated: true)
                    }
                }

            }

            // 双向通信
            let messageChannel = FlutterBasicMessageChannel(name: "BasicMessageChannel", binaryMessenger: binaryMessenger)
            messageChannel.setMessageHandler { obj, reply in
                if let jsonData = try? JSONSerialization.data(withJSONObject: obj ?? [], options: []) {
                    if let jsonString = String(data: jsonData, encoding: .utf8) {
                        let alert = UIAlertController(title: "原生双向通道弹窗", message: "接收参数\(jsonString)", preferredStyle: UIAlertController.Style.alert)
                        alert.addAction(UIAlertAction(title: "发送消息", style: UIAlertAction.Style.destructive, handler: { _ in
                            messageChannel.sendMessage(["data": "原生发送消息到flutter"])
                        }))
                        alert.addAction(UIAlertAction(title: "关闭", style: UIAlertAction.Style.cancel, handler: { _ in

                        }))
                        flutter?.present(alert, animated: true)
                    }
                }
            }
        }

        return super.application(application, didFinishLaunchingWithOptions: launchOptions)
    }

    override func applicationWillTerminate(_ application: UIApplication) {
        super.applicationWillTerminate(application)
    }
}
